package com.ds.lens.data.service.dao;

import com.github.pagehelper.Page;
import com.ds.lens.data.service.dao.entity.LensApplication;
import com.ds.lens.data.service.dao.sql.provider.LensApplicationSqlProvider;
import com.ds.lens.data.vo.in.v2.application.LensApplicationSearchVO;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

/**
 * Description:
 *
 * @author WeiShaoying
 * @date 2019-11-05
 */

@Repository
public interface LensApplicationDao {

    String COLUMNS = " id, name, app_key, creator, created_time, mender, modified_time ";
    String COLUMNS_JOIN = " app.id, app.name, app.app_key as appKey, app.creator, app.created_time as createdTime, app.mender, app.modified_time as modifiedTime";

    @Delete({
            "delete from lens_application",
            "where id = #{id,jdbcType=BIGINT}"
    })
    int deleteByPrimaryKey(Long id);

    @Insert({
            "insert into lens_application (",
            COLUMNS,
            " ) values (#{id,jdbcType=BIGINT}, #{name,jdbcType=VARCHAR}, ",
            "#{appKey,jdbcType=VARCHAR}, #{creator,jdbcType=VARCHAR}, ",
            "#{createdTime,jdbcType=TIMESTAMP}, #{mender,jdbcType=VARCHAR}, ",
            "#{modifiedTime,jdbcType=TIMESTAMP})"
    })
    int insert(LensApplication record);

    @InsertProvider(type = LensApplicationSqlProvider.class, method = "insertSelective")
    int insertSelective(LensApplication record);

    @Select({
            "select",
            COLUMNS,
            "from lens_application",
            "where id = #{id,jdbcType=BIGINT}"
    })
    @Results(id = "lensApplication", value = {
            @Result(column = "id", property = "id", jdbcType = JdbcType.BIGINT, id = true),
            @Result(column = "name", property = "name", jdbcType = JdbcType.VARCHAR),
            @Result(column = "app_key", property = "appKey", jdbcType = JdbcType.VARCHAR),
            @Result(column = "creator", property = "creator", jdbcType = JdbcType.VARCHAR),
            @Result(column = "created_time", property = "createdTime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "mender", property = "mender", jdbcType = JdbcType.VARCHAR),
            @Result(column = "modified_time", property = "modifiedTime", jdbcType = JdbcType.TIMESTAMP)
    })
    LensApplication selectByPrimaryKey(Long id);

    @UpdateProvider(type = LensApplicationSqlProvider.class, method = "updateByPrimaryKeySelective")
    int updateByPrimaryKeySelective(LensApplication record);

    @Update({
            "update lens_application",
            "set name = #{name,jdbcType=VARCHAR},",
            "app_key = #{appKey,jdbcType=VARCHAR},",
            "creator = #{creator,jdbcType=VARCHAR},",
            "created_time = #{createdTime,jdbcType=TIMESTAMP},",
            "mender = #{mender,jdbcType=VARCHAR},",
            "modified_time = #{modifiedTime,jdbcType=TIMESTAMP}",
            "where id = #{id,jdbcType=BIGINT}"
    })
    int updateByPrimaryKey(LensApplication record);

    @Select({
            "<script>",
            "select",
            COLUMNS_JOIN,
            "from lens_application app",
            "where 1 = 1 ",
            "<if test='id != null'> and app.id = #{id,jdbcType=BIGINT} </if>",
            "<if test='name != null'> and app.name = #{name,jdbcType=VARCHAR} </if>",
            "<if test='appKey != null'> and app.app_key = #{appKey,jdbcType=VARCHAR} </if>",
            "<if test='userName != null'> and app.id in (select application_id from lens_application_user_relation where user_name=#{userName,jdbcType=VARCHAR}) </if>",
            "</script>"
    })
    Page<LensApplication> queryLensApplications(LensApplicationSearchVO app);

    @Select({
            "<script>",
            "select",
            COLUMNS_JOIN,
            "from lens_application app ",
            " left join lens_application_user_relation relation on app.id = relation.application_id ",
            " where relation.user_name = #{userName,jdbcType=VARCHAR} ",
            "</script>"
    })
    List<LensApplication> queryLensApplicationsByUserName(@Param("userName") String userName);

    @Select({
            "select",
            " count(1) > 0",
            "from lens_application ",
            " where app_key = #{appKey,jdbcType=VARCHAR} "
    })
    boolean usedAppKey(@Param("appKey") String appKey);

    @Select({
            "select",
            " count(1) > 0",
            "from lens_application ",
            " where name = #{name,jdbcType=VARCHAR} "
    })
    boolean usedAppName(@Param("name") String name);

    @Select({
            "select id, name, modified_time",
            " from lens_application"
    })
    @ResultMap("lensApplication")
    List<LensApplication> listAllApplications();

    @Select({
            "select id, name, modified_time",
            " from lens_application where modified_time > #{modifyTime}"
    })
    @ResultMap("lensApplication")
    List<LensApplication> listUpdatedApplications(@Param("modifyTime") Date modifyTime);


    @Select({
            "<script>",
            "select",
            COLUMNS_JOIN,
            "from lens_application app",
            "where app.id in (select application_id from lens_application_user_relation where user_name=#{userName,jdbcType=VARCHAR}",
            "<if test='userType != null'> and user_type = #{userType,jdbcType=TINYINT} </if>",
            "<if test='appRole != null'> and application_role = #{appRole,jdbcType=TINYINT} </if>",
            ")",
            "</script>"
    })
    List<LensApplication> queryAllLensApplications(@Param("userName") String userName,
                                                   @Param("userType") Integer userType,
                                                   @Param("appRole") Integer appRole);

    @Select({
            "select ",
            COLUMNS,
            " from lens_application",
            "where app_key=#{appKey,jdbcType=VARCHAR}"
    })
    @ResultMap("lensApplication")
    LensApplication queryApplicationByKey(String appKey);

}
